package com.cjl.interview150.binarySearch;

import org.junit.jupiter.api.Test;

import java.util.Arrays;

public class BinarySearchPractice {
    /**
     * 74. 搜索二维矩阵
     *
     * @param matrix
     * @param target
     * @return
     */
    public boolean searchMatrix(int[][] matrix, int target) {
        for (int[] arr : matrix) {
            int left = 0;
            int right = matrix[0].length - 1;
            while (left <= right) {
                // 定义mid
                int mid = left + ((right - left) >> 1);
                if (arr[mid] > target) {
                    right = mid - 1;
                } else if (arr[mid] < target) {
                    left = mid + 1;
                } else {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 162. 寻找峰值
     * @param nums
     * @return
     */
    public int findPeakElement(int[] nums) {
        int left = 0, right = nums.length - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] > nums[mid + 1]) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return left;
    }

    public int[] plusOne(int[] digits) {
        int num = 0;
        for (int digit : digits) {
            num = num * 10 + digit;
        }
        num += 1;
        String str = String.valueOf(num);
        String[] split = str.split("");
        int[] res = new int[split.length];
        for (int i = 0; i < split.length; i++) {
            res[i] = Integer.valueOf(split[i]);
        }
        return res;
    }

    @Test
    public void test() {
        plusOne(new int[]{9,8,7,6,5,4,3,2,1,0});
    }
}
